//
//  HJDPesonalCenterViewController.m
//  JYHospital
//
//  Created by 胡金东 on 16/6/7.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDPesonalCenterViewController.h"
#import "HJDHealthViewController.h"

#import "HJDSetViewController.h"
#import "HJDSetDateViewController.h"
#import "HJDCalculateDateViewController.h"
#import "HJDEVersionViewController.h"
#import "HJDNavigationController.h"
#import "HJDCTimeViewController.h"
#import "HJDUserInfoViewController.h"
#import "HJDLoginViewController.h"
#import "HJDResigerRecordViewController.h"
//#import "AFNetworking.h"
#import "HJDFileViewController.h"

#import "HJDGestationViewController.h"

#import "MBProgressHUD.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <QuartzCore/QuartzCore.h>

#import "BSMacros.h"


@interface HJDPesonalCenterViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    int statusCase;
}

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) UIImageView * imageView;
//昵称
@property (nonatomic,strong) UILabel *nameLbl ;
//预产期
@property (nonatomic,strong) UILabel *timeLbl ;




@end

@implementation HJDPesonalCenterViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.nameLbl.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"changNick"];

        NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
    UIImage  *savedImage = [UIImage imageWithContentsOfFile:fullPath];
      if (!savedImage)
    {
        //默认头像
        self.imageView.image = [UIImage imageNamed:@"新头像"];
    }
    else
    {
        self.imageView.image = savedImage;
    }
    
    NSData *dueData = [[NSUserDefaults standardUserDefaults]valueForKey:@"due"];
    NSString *due = [NSKeyedUnarchiver unarchiveObjectWithData:dueData];
    if (![due isKindOfClass:[NSNull class]]) {
        self.timeLbl.text = [NSString stringWithFormat:@"预产期%@",due];

    }
    }

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self setUpBtn];
    [self setUpTable];
    
}
-(void)setUpBtn{
    

    
    UIView *VB= [[UIView alloc]initWithFrame:CGRectMake(55, 20,IS_IPHONE_4_OR_LESS?205:IS_IPHONE_5?205: IS_IPHONE_6?232.5:IS_IPHONE_6_PLUS?252.5:205 , 80)];
    VB.backgroundColor = [UIColor getColorWithHexNumber:@"F1F1EE"];
    [self.view addSubview:VB];
    
    
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(-40, 0, 80, 80)];
    [self.imageView setRadius:self.imageView.height/2 borderWith:0 borderColor:nil];
    //    加载首先访问本地沙盒是否存在相关图片
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
    UIImage  *savedImage = [UIImage imageWithContentsOfFile:fullPath];
//    NSLog(@"%@",fullPath);

 
        if (!savedImage)
        {
            //默认头像
            self.imageView.image = [UIImage imageNamed:@"新头像"];
        }
        else
        {
            self.imageView.image = savedImage;
        }
    
    [VB addSubview:self.imageView];
    NSUserDefaults *defalt = [NSUserDefaults standardUserDefaults];
    NSString *changNick = [defalt objectForKey:@"changNick"];
    
    self.nameLbl = [[UILabel alloc]initWithFrame:CGRectMake(45, 10, 200, 30)];
    
        self.nameLbl.text = changNick;
      [VB addSubview:self.nameLbl];
    
     self.timeLbl = [[UILabel alloc]initWithFrame:CGRectMake(45, CGRectGetMaxY(self.nameLbl.frame) , 200, 30)];
    NSUserDefaults *defaD = [NSUserDefaults standardUserDefaults];
    NSString *preD = [defaD objectForKey:@"pregnanc"];
    
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *inputDate = [inputFormatter dateFromString:preD];
    NSDate *nextDate = [NSDate dateWithTimeInterval:24*60*60 * 280 sinceDate:inputDate];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *strDate = [dateFormatter stringFromDate:nextDate];
    NSLog(@"%@", strDate);
    
    self.timeLbl.text = [NSString stringWithFormat:@"预产期%@",strDate];

    [VB addSubview:self.timeLbl];
    
        UIButton *resiBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 288,80 )];
        [resiBtn addTarget:self action:@selector(resiBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:resiBtn];
   
}

-(void)resiBtn
{
    HJDUserInfoViewController *userInfo = [[HJDUserInfoViewController alloc]init];
    HJDNavigationController *nav = [[HJDNavigationController alloc]initWithRootViewController:userInfo];
    
    [self presentViewController:nav animated:YES completion:nil];
}


-(void)setUpTable
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 120,260 , 700)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    [self.view addSubview:self.tableView];
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0){
        return 3;
    }else {
        return 2;
   
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    if (indexPath.section == 0) {
        if (indexPath.row ==0) {
            cell.imageView.image = [UIImage imageNamed:@"孕期档案"];
            cell.textLabel.text = @"档案管理";
//        }else if (indexPath.row ==1){
//            cell.imageView.image = [UIImage imageNamed:@"我的论坛"];
//            cell.textLabel.text = @"我的论坛";
//        }else if (indexPath.row == 2) {
//            cell.imageView.image = [UIImage imageNamed:@"会员专区"];
//            cell.textLabel.text = @"会员专区";
        }else if (indexPath.row == 1){
            cell.imageView.image = [UIImage imageNamed:@"产检记录"];
            cell.textLabel.text = @"产检记录";
        }else{
        cell.imageView.image = [UIImage imageNamed:@"挂号记录"];
        cell.textLabel.text = @"挂号记录";
        }

        
    }else if (indexPath.section == 1){
        
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"设置"];
            cell.textLabel.text = @"设置";
        }else{
            cell.imageView.image = [UIImage imageNamed:@"版本更新"];
            cell.textLabel.text = @"版本更新";
        }
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
            
            statusCase = [[[NSUserDefaults standardUserDefaults]stringForKey:@"stusCase"] intValue];
            
//            NSLog(@"statusCase =%d",statusCase);
            if (statusCase ==0) {
                HJDGestationViewController *gestation = [[HJDGestationViewController alloc]init];
                HJDNavigationController *nav = [[HJDNavigationController alloc]initWithRootViewController:gestation];
                [self presentViewController:nav animated:YES completion:nil];
            }else if (statusCase ==1){
                HJDFileViewController *file = [[HJDFileViewController alloc]init];
                HJDNavigationController *nav = [[HJDNavigationController alloc]initWithRootViewController:file];
                [self presentViewController:nav animated:YES completion:nil];
            }

        }else if (indexPath.row == 1){
            HJDCTimeViewController *cTime = [[HJDCTimeViewController alloc]init];
            HJDNavigationController *nav = [[HJDNavigationController alloc]initWithRootViewController:cTime];
            
            [self presentViewController:nav animated:YES completion:nil];

        }else {
            HJDResigerRecordViewController *resigerRecord = [[HJDResigerRecordViewController alloc]init];
            HJDNavigationController *nav = [[HJDNavigationController alloc]initWithRootViewController:resigerRecord];
            
            [self presentViewController:nav animated:YES completion:nil];
        }
        
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            HJDSetViewController *set = [[HJDSetViewController alloc]init];
            
            HJDNavigationController *nav = [[HJDNavigationController alloc]initWithRootViewController:set];
            
            [self presentViewController:nav animated:YES completion:nil];
        }else{

            //1.获取当前的版本号
            NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
            //2.获取上一次的版本号
                NSString *lastVersion = [[NSUserDefaults standardUserDefaults]objectForKey:HJDVersionKey];
            [MBProgressHUD showMessage:@"正在检查更新..."];
            
            if ([currentVersion isEqualToString:lastVersion]) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [MBProgressHUD hideHUD];
                    [MBProgressHUD showSuccess:@"当前已经是最新版本"];
                    
                });
                
            }
        }
      }
    
}


@end
